package com.hlw.hr_man.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hlw.hr_man.dao.FuncDao;
import com.hlw.hr_man.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 功能表的dao层实现
 * @author Administrator
 *
 */
public class FuncDaoImpl  implements FuncDao{
/**
 * 找到当前登录用户拥有的功能
 */
	@Override
	public String getCurrentEmployeeFuncs(int eid) {
		String funcs=null;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select role_funcs from hr_employee ,hr_role where hr_role.role_id=hr_employee.role_id and hr_employee.e_id=?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, eid);
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				funcs=rs.getString("role_funcs");
			}
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return funcs;
	}
/**
 * 通过父节点找功能并存进json中
 */
	@Override
	public JSONArray getfuncsByParentId(int parentId, String sfunc) {
		JSONArray funcs=new JSONArray();
		Connection  con   =  JDBCUtil.getConnection();
		StringBuffer  sql = new StringBuffer("select * from hr_func where func_parentId=? and func_id in (");
		sql.append(sfunc);
		sql.append(")");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			pst.setInt(1, parentId);
			ResultSet  rs  =  pst.executeQuery();
			while(rs.next()){ 
				  JSONObject  func =  new JSONObject();
				  func.put("id", rs.getInt("func_id"));
				  func.put("text", rs.getString("func_name"));
				  func.put("state", rs.getString("func_state"));
				  JSONObject  attr  = new JSONObject();
				  attr.put("fpath", rs.getString("func_path"));
				  func.put("attributes", attr);
				  funcs.add(func);
			}
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return funcs;
	}
/**
 * 通过遍历找到功能树
 */
	@Override
	public JSONArray getCurrentUserFuncsByParentId(int parentId, String sfunc) {
		JSONArray   employeefuncs  = this.getfuncsByParentId(parentId,sfunc);
		for (int i = 0; i < employeefuncs.size(); i++) {
			  String state = employeefuncs.getJSONObject(i).getString("state");
			  if("open".equals(state)){
				     continue;
			  }else{
				   int currentId = employeefuncs.getJSONObject(i).getInt("id");
				   JSONArray   children  =  this.getCurrentUserFuncsByParentId(currentId,sfunc);
				   employeefuncs.getJSONObject(i).put("children", children);
			  }
		}
		
		
		
		return  employeefuncs;
	}
@Override
public String getCurrentEmployeeFuncs(String enumber) {
	String funcs=null;
	Connection con=JDBCUtil.getConnection();
	StringBuffer sql=new StringBuffer("select role_funcs from hr_employee ,hr_role where hr_role.role_id=hr_employee.role_id and hr_employee.e_number=?");
	try {
		PreparedStatement pst=con.prepareStatement(sql.toString());
		pst.setString(1, enumber);
		ResultSet rs=pst.executeQuery();
		if(rs.next()){
			funcs=rs.getString("role_funcs");
		}
		JDBCUtil.closeJDBC(pst, con);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return funcs;
}
@Override
public String getCurrentEmployeeFuncsByname(String rname) {
	String funcs=null;
	Connection con=JDBCUtil.getConnection();
	StringBuffer sql=new StringBuffer("select role_funcs from hr_role where hr_role.role_name=?");
	try {
		PreparedStatement pst=con.prepareStatement(sql.toString());
		pst.setString(1, rname);
		ResultSet rs=pst.executeQuery();
		if(rs.next()){
			funcs=rs.getString("role_funcs");
		}
		JDBCUtil.closeJDBC(pst, con);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return funcs;
}

}
